package net.st.model;

import java.io.Serializable;
import java.util.Date;

import net.st.config.StConfig;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.CreationDate;
import org.slim3.datastore.Model;
import org.slim3.datastore.ModificationDate;

import com.google.appengine.api.datastore.Key;

@Model(schemaVersion = 1)
public class SearchData implements Serializable {

	private static final long serialVersionUID = 1L;

	@Attribute(primaryKey = true)
	private Key key;

	@Attribute(version = true)
	private Long version;

	private Key userKey;

	private Key engineTemplateKey;

	private String query;

	private String engineName;

	private String url;

	private String newHtmlDataXPath;

	private String md5XPath;

	@Attribute(lob = true)
	private String oldHtmlData;

	@Attribute(lob = true)
	private String newHtmlData;

	@Attribute(lob = true)
	private String diffHtmlData;

	private String md5;

	private Boolean newFlag;

	private Date refreshDate;

	@Attribute(listener = ModificationDate.class)
	private Date updateDate;

	@Attribute(listener = CreationDate.class)
	private Date insertDate;

	/**
	 * Returns the key.
	 * 
	 * @return the key
	 */
	public Key getKey() {
		return key;
	}

	/**
	 * Sets the key.
	 * 
	 * @param key
	 *            the key
	 */
	public void setKey(Key key) {
		this.key = key;
	}

	/**
	 * Returns the version.
	 * 
	 * @return the version
	 */
	public Long getVersion() {
		return version;
	}

	/**
	 * Sets the version.
	 * 
	 * @param version
	 *            the version
	 */
	public void setVersion(Long version) {
		this.version = version;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		this.md5 = md5;
	}

	public Date getInsertDate() {
		return insertDate;
	}

	public void setInsertDate(Date insertDate) {
		this.insertDate = insertDate;
	}

	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		SearchData other = (SearchData) obj;
		if (key == null) {
			if (other.key != null) {
				return false;
			}
		} else if (!key.equals(other.key)) {
			return false;
		}
		return true;
	}

	public String getEngineName() {
		return engineName;
	}

	public void setEngineName(String engineName) {
		this.engineName = engineName;
	}

	public String getNewHtmlDataXPath() {
		return newHtmlDataXPath;
	}

	public void setNewHtmlDataXPath(String newHtmlDataXPath) {
		this.newHtmlDataXPath = newHtmlDataXPath;
	}

	public String getMd5XPath() {
		return md5XPath;
	}

	public void setMd5XPath(String md5xPath) {
		md5XPath = md5xPath;
	}

	public String getOldHtmlData() {
		return oldHtmlData;
	}

	public void setOldHtmlData(String oldHtmlData) {
		this.oldHtmlData = oldHtmlData;
	}

	public String getNewHtmlData() {
		return newHtmlData;
	}

	public void setNewHtmlData(String newHtmlData) {
		this.newHtmlData = newHtmlData;
	}

	public String getDiffHtmlData() {
		return diffHtmlData;
	}

	public void setDiffHtmlData(String diffHtmlData) {
		this.diffHtmlData = diffHtmlData;
	}

	public Date getRefreshDate() {
		return refreshDate;
	}

	public void setRefreshDate(Date refreshDate) {
		this.refreshDate = refreshDate;
	}

	public Key getUserKey() {
		return userKey;
	}

	public void setUserKey(Key userKey) {
		this.userKey = userKey;
	}

	public Key getEngineTemplateKey() {
		return engineTemplateKey;
	}

	public void setEngineTemplateKey(Key engineTemplateKey) {
		this.engineTemplateKey = engineTemplateKey;
	}

	public Boolean getNewFlag() {
		return newFlag;
	}

	public void setNewFlag(Boolean newFlag) {
		this.newFlag = newFlag;
	}
	
	public String getWebPage() {
		return getUrl().replace(StConfig.TAG_QUERY, getQuery());
	}
}
